package com.ubadong.studentmng.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ubadong.studentmng.service.StudentService;
import com.ubadong.studentmng.service.impl.StudentServiceImpl;
import com.ubadong.studentmng.vo.Student;

public class StudentMNGServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
//		req.setCharacterEncoding("UTF-8");
//		resp.setCharacterEncoding("UTF-8");
		StudentService service = new StudentServiceImpl();
		
		String method = req.getParameter("method");
		
		if("list".equals(method)){
			Student studentPager = new Student();
			
			String gender = req.getParameter("gender");
			studentPager.setName(req.getParameter("name"));
			studentPager.setGender(gender==null?-1:Integer.parseInt(gender));
			studentPager.setEdu(req.getParameter("edu"));
			//设置当前页
			int currentPage = req.getParameter("currentPage")==null?1:Integer.valueOf(req.getParameter("currentPage"));
			studentPager.setCurrentPage(currentPage);
			int counts = service.countAllStudent(studentPager);
			//设置总记录数，用于分页
			studentPager.setCounts(counts);
			List list = service.getAllStudent(studentPager);
			
			req.setAttribute("result", list);
			req.setAttribute("page", studentPager);
			
			req.getRequestDispatcher("student/student_list.jsp").forward(req, resp);
		}
		if("add".equals(method)){
			Student student = new Student();
			student.setName(req.getParameter("name"));
			student.setGender(Integer.parseInt(req.getParameter("gender")));
			student.setEdu(req.getParameter("edu"));
			
			student.setPasswd(req.getParameter("passwd"));
			String age = req.getParameter("age");
			student.setAge(age==null ||age.length()==0?0:Integer.parseInt(age));
			
			
			//组合爱好
			String[] hobbys = req.getParameterValues("hobby");
			if(hobbys != null){
				String hobby = "";
				for (int i = 0; i < hobbys.length; i++) {
					if(i==hobbys.length-1){
						hobby = hobby+hobbys[i];
					}else{
						hobby = hobby+hobbys[i]+",";
					}
				}
				student.setHobby(hobby);
			}
			
			//日期
			String birthday = req.getParameter("birthday");
			student.setBirthday(birthday);
			
			student.setRemark(req.getParameter("remark"));
			
			service.addStduent(student);
			resp.sendRedirect("studentMNGServlet?method=list");
		}
		
		if("edit".equals(method)){
			
			int id = Integer.parseInt(req.getParameter("id"));
			Student s = service.getStudentById(id);
			req.setAttribute("student", s);
			req.getRequestDispatcher("student/student_edit.jsp").forward(req, resp);
		}
		
		if("update".equals(method)){
			Student student = new Student();
			student.setId(Integer.valueOf(req.getParameter("id")));
			student.setName(req.getParameter("name"));
			student.setGender(Integer.parseInt(req.getParameter("gender")));
			student.setEdu(req.getParameter("edu"));
			
			student.setPasswd(req.getParameter("passwd"));
			String age = req.getParameter("age");
			student.setAge(age==null ||age.length()==0?0:Integer.parseInt(age));
			student.setHobby(req.getParameter("hobby"));
			
			String birthday = req.getParameter("birthday");
			student.setBirthday(birthday);

			student.setRemark(req.getParameter("remark"));
			
			service.updateStduent(student);
			resp.sendRedirect("studentMNGServlet?method=list");
		}

		if("delete".equals(method)){
			String ids = req.getParameter("ids");
			service.deleteStduent(ids);
			resp.sendRedirect("studentMNGServlet?method=list");
		}
	}
}

